package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.dto.AdvimgDto;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.mapper.YddAdvimgMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.util.MyPages;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {

	@Autowired
	private YddAdvposService yddAdvposService;

	@Override
	public MyPages<AdvimgDto> listByPage(String advimgdesc, String advimgpos, String page, String limit) {
		Page<YddAdvimg> page1 = new Page<>(Long.parseLong(page), Long.parseLong(limit));
		MyPages<AdvimgDto> myPage = new MyPages<>();
		LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc)
				.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
		this.page(page1, wrapper);

		List<AdvimgDto> myRecords = new ArrayList<>();
		List<YddAdvimg> records = page1.getRecords();
		for (YddAdvimg record : records) {
			AdvimgDto advimgDto = new AdvimgDto();
			BeanUtils.copyProperties(record, advimgDto);
			String advposname = yddAdvposService.getById(record.getAdvimgpos()).getAdvposname();
			advimgDto.setAdvposname(advposname);
			myRecords.add(advimgDto);
		}

		myPage.setCount(page1.getTotal());
		myPage.setTotalPages(page1.getPages());
		myPage.setPageSize(page1.getSize());
		myPage.setCurrentPage(page1.getCurrent());
		myPage.setData(myRecords);
		return myPage;
	}
}
